package com.suep.lesson_manage.application.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suep.lesson_manage.application.dto.AddLessonRequestDto;
import com.suep.lesson_manage.application.dto.LessonDto;
import com.suep.lesson_manage.application.dto.UpdateLessonRequestDto;
import com.suep.lesson_manage.application.mapper.LessonMapper;
import com.suep.lesson_manage.model.LessonSection;
import com.suep.lesson_manage.repository.LessonRepository;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class LessonService {

    private final LessonRepository lessonRepository;
    private final LessonMapper lessonMapper;
    //根据课节号获取课节信息
    public LessonDto getLessonById(Integer section_id){
        Objects.requireNonNull(section_id,"section_id cannot be null");

        //build mybatis-plus query condition
        Wrapper<LessonSection> query= Wrappers.<LessonSection>lambdaQuery().eq(LessonSection::getSection_id,section_id);

        //fetch by query condition
        return lessonRepository.selectByid(query).stream().
                map(lessonMapper::toLessonDto).
                findFirst()
                .orElse(null);
    }
    //获取所有课节信息
    public List<LessonDto> getAllLessons(){
        return lessonRepository.selectAll().stream().
                map(lessonMapper::toLessonDto).
                collect(Collectors.toList());
    }

    // 获取课节表的行数
    public long getLessonSectionCount() {
        return lessonRepository.count();
    }
    //新增课节信息
    @Transactional
    public String addLesson(AddLessonRequestDto addLessonRequestDto){
        //do validation
        Objects.requireNonNull(addLessonRequestDto,"addLessonRequestDto cannot be null");
        //build lesson object
        var targertLesson = LessonSection.builder()
                .section_id(addLessonRequestDto.getSection_id())
                .section_name(addLessonRequestDto.getSection_name())
                .start_time(addLessonRequestDto.getStart_time())
                .end_time(addLessonRequestDto.getEnd_time())
                .duration(addLessonRequestDto.getDuration())
                .build();
        //save lesson object
        if (this.lessonRepository.insert(targertLesson)<=0){
            throw new RuntimeException("Failed to add lesson");
        }else{
            return "success";
        }
    }
    //删除课节信息
    public void removeLesson(Integer section_id){
        //do validation
        Objects.requireNonNull(section_id,"section_id cannot be null");
        //do delete
        this.lessonRepository.deleteById(section_id);
    }
    //更新课节信息
    @Transactional
    public String updateLesson(Integer section_id,UpdateLessonRequestDto updateLessonRequestDto){
        //do validation
        Objects.requireNonNull(updateLessonRequestDto,"updateLessonRequestDto cannot be null");
        //build teacher object
        var targetLesson = LessonSection.builder().section_id(section_id).build();
        if(updateLessonRequestDto.getSection_name()!=null){
            targetLesson.setSection_name(updateLessonRequestDto.getSection_name());
        }
        if(updateLessonRequestDto.getStart_time()!=null){
            targetLesson.setStart_time(updateLessonRequestDto.getStart_time());
        }
        if (updateLessonRequestDto.getEnd_time()!=null){
            targetLesson.setEnd_time(updateLessonRequestDto.getEnd_time());
        }
        if (updateLessonRequestDto.getDuration()!=null){
            targetLesson.setDuration(updateLessonRequestDto.getDuration());
        }

        //update teacher object
        if (this.lessonRepository.updateById(targetLesson)<=0){
            throw new RuntimeException("Failed to update lesson");
        }else{
            return "success";
        }
    }
}
